package cn.legend.travel.passport.controller;

import cn.legend.travel.common.pojo.authentication.CurrentPrincipal;
import cn.legend.travel.common.web.JsonResult;
import cn.legend.travel.passport.pojo.param.UserLoginInfoParam;
import cn.legend.travel.passport.pojo.param.UserRegInfoParam;
import cn.legend.travel.passport.pojo.vo.UserLoginResultVO;
import cn.legend.travel.passport.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Pattern;

@Api(tags = "1.单点登录")
@Validated
@RestController
@RequestMapping("/passport/")
public class UserController {
    @Autowired
    private IUserService iUserService;
    @ApiOperation(value = "登录")
    @ApiOperationSupport(order = 1)
    @PostMapping("login")
    public JsonResult login(@Validated UserLoginInfoParam userLoginInfoParam,
                            @ApiIgnore HttpServletRequest request
    ){
//        System.out.println("UserLoginInfoParam:"+userLoginInfoParam);
        UserLoginResultVO userLoginResultVO = iUserService.login(userLoginInfoParam,request);
//        System.out.println("userLoginResultVO"+userLoginResultVO);
        return JsonResult.ok(userLoginResultVO);
    }

    @ApiOperation(value = "手机验证码登录")
    @ApiOperationSupport(order = 2)
    @PostMapping("login-by-phone")
    public JsonResult loginByPhone(@Pattern(regexp = "^\\d{11}$",message = "手机号码必为11位数字！") String phone,
                                   @ApiIgnore HttpServletRequest request
    ){
//        System.out.println("phone:"+phone);
        UserLoginResultVO userLoginResultVO = iUserService.loginByPhone(phone,request);
//        System.out.println("userLoginResultVO"+userLoginResultVO);
        return JsonResult.ok(userLoginResultVO);
    }

    @ApiOperation(value = "注册")
    @ApiOperationSupport(order = 3)
    @PostMapping("reg")
    public JsonResult reg(@Validated UserRegInfoParam userRegInfoParam,
                            @ApiIgnore HttpServletRequest request
    ){
//        System.out.println("userRegInfoParam:"+userRegInfoParam);
        iUserService.reg(userRegInfoParam);
        return JsonResult.ok();
    }

    @ApiOperation(value = "退出登录")
    @ApiOperationSupport(order = 4)
    @PostMapping("logout")
    public JsonResult logout(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        iUserService.logout(currentPrincipal);
        return JsonResult.ok();
    }

    @ApiOperation(value = "发送验证码")
    @ApiOperationSupport(order = 5)
    @PostMapping("sendCode")
    public JsonResult sendCode(@Pattern(regexp = "^\\d{11}$",message = "手机号格式11位数字！") String phone){
//        System.out.println("controller接收手机号："+phone);
        long l=System.currentTimeMillis();
//        System.out.println("System.currentTimeMillis："+l);
        int i=(int)l%10000;
        String code = String.valueOf(i);

        //返回0，发送验证码；返回1则输入手机号码
        if (phone!=null){
            iUserService.send(phone,code);
            return JsonResult.ok(0);
        }
        return JsonResult.ok(1);
    }


    @ApiOperation(value = "判断手机验证码的正确性")
    @ApiOperationSupport(order = 6)
    @RequestMapping("compareCode")
    @ResponseBody
    public JsonResult authorization(@Pattern(regexp = "^\\d{4}$",message = "手机验证码必为4位数字！")String preAuthCode,
                             @Pattern(regexp = "^\\d{11}$",message = "手机号码必为11位数字！") String phone) {
        int reuslt = iUserService.compareCode(phone,preAuthCode);
        return JsonResult.ok(reuslt);
    }



}


